function [mdl] = MDL(neuralGasMap)
KAPPA=0.8;
ETA=0.000005;
COSINE=neuralGasMap.COSINE;

D=neuralGasMap.D;
M=neuralGasMap.M;
DIM=size(M,2);
c=size(M,1);
N=size(D,1);



range=max(max(D))-min(min(D));
K=DIM*ceil(log2(range/ETA)+1);



%find outliers
%data-BMU hits/needed for 
if (COSINE == 0)
    [hits,distances] = neuralGasBmus(M,D);
else
    [hits,distances] = neuralGasBmusCosine(M,D);
end

[temp,indeces] = sort(distances);
%hits=hits(indeces);
    
outliers = zeros(size(D,1),1);
    
%for i=1:size(D,1)
%    x=D(i,:);
%    unit = hits(i);
%    numberOfDataByUnit = sum(hits==unit);
%    phi = numberOfDataByUnit == 1;
%    w=M(unit,:);
    
    %indexchange = log2(protosize)+(size(dataset,1)-size(outdata,1)-1)*(log2(protosize)-log2(protosize-1));
    %log(c) + N-1*(log(c)-log(c-1))
%    value = (size(dataset,1)-sum(outliers)-1)*(log2(c-phi))+K - (size(dataset,1)-sum(outliers)-1)*log2(c);
%    for k=1:DIM
%        value = value - max(log2((abs(x(k)-w(k))/ETA)),1);
%    end
%    value = value*kappa - phi*K;
%    if (value<0)
%        outliers(i)=1;
%    else
%        outliers(i)=0;
%   end
    
%end

%calc MDL
%mod L(I,w) + error L(I,W) + mod L(O)

%MOD_LIW
%N=size(D,1)-sum(outliers);



%MOD_ERROR
ERROR_LIW=0;
dataCoveredByUnit=zeros(size(M,1),1);

for i=1:size(M,1)
   dataCoveredByUnit(i)=sum(hits==i); 
end

dataVectors=1:size(D,1);

for i=1:length(indeces)
    d = indeces(i);
    unit = hits(d);
    w = M(unit,:);
    
    error=0;
    x=D(d,:);
        
    for k=1:DIM
        error = error + max(log2((abs(x(k)-w(k))/ETA)),1);
    end
        
    phi = dataCoveredByUnit(unit)  == 1;
       
    value = log(c) + (size(D,1)-sum(outliers)-1)*(log2(c)-log2(c-phi));
        
    if (KAPPA*error+value+phi*K>K)
        outliers(d)=1;
        dataCoveredByUnit(unit)=dataCoveredByUnit(unit)-1;
    else
        ERROR_LIW=ERROR_LIW+error;
    end;
        
end

MOD_LIW_CK = c*K;
MOD_LIW_NLOGC = (size(D,1)-sum(outliers))*log2(c+1);
MOD_LIW=MOD_LIW_CK+MOD_LIW_NLOGC;

MOD_LO = sum(outliers)*K;
sum(outliers);


mdl = MOD_LIW + KAPPA*ERROR_LIW + MOD_LO;



 

